package com.gs.api.projects.shejiao.web;


import cn.binarywang.wx.miniapp.api.WxMaQrcodeService;
import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaCodeLineColor;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gs.api.config.weixin.WxMaConfiguration;
import com.gs.api.core.Result;
import com.gs.api.core.ResultCode;
import com.gs.api.core.common.oss.FilesService;
import com.gs.api.core.common.util.ShareCodeUtil;
import com.gs.api.projects.shejiao.dao.TMemberUserMapper;
import com.gs.api.projects.shejiao.entity.TMemberUser;
import com.gs.api.projects.shejiao.service.ITMemberUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import me.chanjar.weixin.common.error.WxErrorException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author guoshao
 * @since 2021-10-12
 */
@Api(tags = "推广会员")
@RestController
@RequestMapping("{appid}/member")
public class TMemberUserController {

    @Autowired
    private ITMemberUserService itMemberUserService;

    @Autowired
    private FilesService filesService;

    @ApiOperation(value = "获取配置信息", notes="获取配置信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "appid", value = "小程序appid", required = true, dataType = "String"),
            @ApiImplicitParam(name = "openid", value = "openid", required = true, dataType = "String"),
    })
    @GetMapping("/getMemberUser")
    public Result getMemberUser(@PathVariable String appid,String openid){

        TMemberUser memberUser = itMemberUserService.getOne(new QueryWrapper<TMemberUser>().eq("user_id", openid));
        if(null == memberUser){
            return Result.failure("找不到相应的数据");
        }
        return Result.success(memberUser);
    }


    @ApiOperation(value = "获取分享二维码", notes="获取分享二维码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "appid", value = "小程序appid", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "openid", value = "openid", required = true, dataType = "Integer"),
    })
    @GetMapping("/getWxQrCode")
    private Result wxacode(@PathVariable  String appid,String openid) throws WxErrorException, IOException {

        TMemberUser memberUser = itMemberUserService.getOne(new QueryWrapper<TMemberUser>().eq("user_id", openid));

        if(null != memberUser){
            if(StringUtils.isNotBlank(memberUser.getQrCode())){
                return Result.success(memberUser.getQrCode());
            }
        }

        String shareCode = memberUser.getShareCode();
        final WxMaService wxService = WxMaConfiguration.getMaService(appid);
        // 设置小程序二维码线条颜色为黑色
        //WxMaCodeLineColor lineColor = new WxMaCodeLineColor("0", "0", "0");
        // 获取小程序二维码生成实例
        WxMaQrcodeService wxMaQrcodeService = wxService.getQrcodeService();

        //File file = wxMaQrcodeService.createWxaCodeUnlimit(""+shareCode,"pages/login/login" ,  430, false, lineColor, false);

        File file = wxMaQrcodeService.createQrcode("pages/login/login?shareCode="+shareCode,430);

        //File.separatorChar返回window系统的"\"或者unix"/"
        String s = filesService.ossResourceUrl() + File.separatorChar + filesService.uploadFileQiniu(file);

        if(null != s){
            memberUser.setQrCode(s);
            itMemberUserService.updateById(memberUser);
        }

        return Result.success(s);
    }

}
